package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.cache.preference.EntityCache;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.PlayerModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 活动数据处理
 *
 * @author HQ
 *         created at 2017/4/25 0025 16:06
 */
public class PlayerPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;
    private EntityCache<PlayerModel> mPlayCache;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
        mPlayCache = new EntityCache<PlayerModel>(mContext, PlayerModel.class, "");
    }

    /**
     * 获得活动列表
     */
    public void getPlayers(boolean flag) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            mView.updateViewWithTag("",ConstantTag.DATA_ERROR);
            return;
        }
        if (flag) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        }
        Call<ResponseData<List<PlayerModel>>> call = mUserService.lotteryPrize(2002);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<PlayerModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<PlayerModel>>> call, Response<ResponseData<List<PlayerModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<PlayerModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<PlayerModel> cards = res.getData();
                        if (null != cards) {
                            if (!cards.isEmpty()) {
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                                mView.updateViewWithTag(cards, ConstantTag.OIL_PLAYERS);
                                try {
                                    mPlayCache.putListEntity(cards);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                return;
                            } else {
                                mPlayCache.putListEntity(new ArrayList<PlayerModel>());
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL);
                                mView.updateViewWithTag("",ConstantTag.DATA_SUCCESS_NULL);
                                return;
                            }
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
                mView.updateViewWithTag("",ConstantTag.DATA_ERROR);
            }

            @Override
            public void onFailure(Call<ResponseData<List<PlayerModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
                mView.updateViewWithTag("",ConstantTag.DATA_ERROR);
            }
        });
    }

    @Override
    public void detachView() {

    }

    @Override
    public List<PlayerModel> getCacheData() {
        return mPlayCache.getListEntity(PlayerModel.class);
    }
}
